package cn.mw.cmdb.enums;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

import static cn.mw.cmdb.service.impl.InstanceServiceImpl.*;

public enum QueryRoomCabinetType {
    QueryInstanceByRoom("查询机房下实例数据", "queryInstanceByRoom") {
        @Override
        public Query query(List ids) throws Exception {
            Query query = new Query(Criteria.where(relationRoomIdPath).in(ids));
            query.addCriteria(Criteria.where(relationCabinetIdPath).exists(true));
            query.addCriteria(Criteria.where(deviceUIndexPath).exists(true));
            return query;
        }
    },
    QueryCabinetByRoom("查询机房下机柜数据", "queryCabinetByRoom") {
        @Override
        public Query query(List ids) throws Exception {
            Query query = new Query(Criteria.where(relationRoomIdPath).in(ids));
            query.addCriteria(Criteria.where(cabinetPositionPath).exists(true));
            query.addCriteria(Criteria.where(cabinetUNumPath).exists(true));
            return query;
        }
    },
    QueryInstanceByCabinet("查询机柜下实例数据", "queryInstanceByCabinet") {
        @Override
        public Query query(List ids) throws Exception {
            Query query = new Query(Criteria.where(relationCabinetIdPath).in(ids));
            query.addCriteria(Criteria.where(relationRoomIdPath).exists(true));
            query.addCriteria(Criteria.where(deviceUIndexPath).exists(true));
            return query;
        }
    };

    private String desc;
    private String type;

    public abstract Query query(List ids) throws Exception;

    QueryRoomCabinetType(String desc, String type) {
        this.desc = desc;
        this.type = type;
    }

    public String getDesc() {
        return this.desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public static Query getQueryType(String type, List ids) throws Exception {
        for (QueryRoomCabinetType r : QueryRoomCabinetType.values()) {
            if (r.getType().equals(type)) {
                return r.query(ids);
            }
        }
        return null;
    }
}
